
#include <dirent.h>

void myfilerecursive(char *path, char *content);

void myfilerecursive(char *basePath, char *content)
{
    // 路径
    char path[64];
    struct dirent *dp;
    DIR *dir = opendir(basePath);

    // 如果是文件则直接结束此次调用
    if (!dir)
    {
        return;
    }
    int index = 0;
    while ((dp = readdir(dir)) != NULL)
    {
        // 跳过 "." 和 ”..“
        if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
        {

            strcpy(path, basePath);

            strcat(path, dp->d_name);
            FILE *fp;
            fp = fopen(path, "r");
            char buf_s[3096] = {0};
            char buf_new[3096] = {0};

            while (!feof(fp)) //没有到文件末尾
            {

                memset(buf_s, 0, sizeof(buf_s));
                fread(buf_s, sizeof(char), sizeof(buf_s), fp);
                strcpy(buf_s, divMail(buf_s, 2));
                // get plain content
                int count1 = 0;
                char seg[] = "\r\n";
                char *token1;
                token1 = strtok(buf_s, seg);
                while ((token1 = strtok(NULL, seg)) != NULL)
                {
                    count1++;
                    if (count1 >= 2)
                    {
                        // base64转ascii
                        strcat(buf_new, base64_decode((unsigned char *)token1));
                    }
                }

                if (strstr(buf_new, content) != NULL)
                {
                    printf("%s\n", dp->d_name);
                    fflush(stdout);
                    index++;
                    break;
                }
            }
            bzero(buf_new, strlen(buf_s));
            fclose(fp);
            // 递归遍历
            myfilerecursive(path, content);
        }
    }
    if (index == 0)
    {
        printf("\nNo result\n");
        fflush(stdout);
    }
    closedir(dir);
}
